﻿using Prism.Commands;
using Prism.Mvvm;
using RSDesktop.Views.Popup;
using RSFrame.Services;
using RSModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace RSDesktop.ViewModels.Popup
{
    public class AssetsInfoViewModel : BindableBase
    {
        public Company CurrentCompany { get; set; }

        private List<Car> currentCompanyCars;
        public List<Car> CurrentCompanyCars
        {
            get { return currentCompanyCars; }
            set { SetProperty(ref currentCompanyCars, value); }
        }

        private List<Driver> currentCompanyDrivers;
        public List<Driver> CurrentCompanyDrivers 
        {
            get { return currentCompanyDrivers; }
            set { SetProperty(ref currentCompanyDrivers, value); }
        }

        public ICarService CarService { get; set; }

        public IDriverService DriverService { get; set; }

        private string lic;
        public string Lic
        {
            get { return lic; }
            set { SetProperty(ref lic, value); }
        }

        private string load;
        public string Load
        {
            get { return load; }
            set { SetProperty(ref load, value); }
        }


        public DelegateCommand<object> AddDriverCommand { get; set; }

        public DelegateCommand<object> DeleteDriverCommand { get; set; }

        public DelegateCommand<object> AddCarCommand { get; set; }

        public DelegateCommand<object> DeleteCarCommand { get; set; }

        public AssetsInfoViewModel(Company company, ICarService carService, IDriverService driverService)
        {
            CurrentCompany = company;
            CarService = carService;
            DriverService = driverService;
            CurrentCompanyCars = CarService.GetCarsByCompany(CurrentCompany.ID);
            CurrentCompanyDrivers = DriverService.GetDriversByCompanyID(CurrentCompany.ID);
            AddDriverCommand = new DelegateCommand<object>(ExecAddDriver);
            DeleteDriverCommand = new DelegateCommand<object>(ExecDeleteDriver);
            AddCarCommand = new DelegateCommand<object>(ExecAddCar);
            DeleteCarCommand = new DelegateCommand<object>(ExecDeleteCar);
        }

        private Task<List<Driver>> GetDriversByCompanyIDAsync()
        {
            var result = new TaskCompletionSource<List<Driver>>();
            new Thread(() =>
            {
                result.SetResult(DriverService.GetDriversByCompanyID(CurrentCompany.ID));
            })
            { IsBackground = true }.Start();
            return result.Task;
        }

        private Task<List<Car>> GetCarsByCompanyIDAsync()
        {
            var result = new TaskCompletionSource<List<Car>>();
            new Thread(() =>
            {
                result.SetResult(CarService.GetCarsByCompany(CurrentCompany.ID));
            })
            { IsBackground = true }.Start();
            return result.Task;
        }

        private async void ExecAddDriver(object parameters)
        {
            string n = parameters as string;
            if (!string.IsNullOrWhiteSpace(n))
            {
                int? r = await AddDriverAsync(n.Trim());
                if (r == null)
                    new TipWindow("警告", "添加失败。请检查网络连接或用户权限").Show();
                else
                {
                    new TipWindow("提示", $"添加成功，员工ID为{r}").Show();
                    CurrentCompanyDrivers = await GetDriversByCompanyIDAsync();
                }
            }
            else
                new TipWindow("提示", "请填入正确的参数").Show();
        }

        private Task<int?> AddDriverAsync(string name)
        {
            var result = new TaskCompletionSource<int?>();
            new Thread(() =>
            {
                result.SetResult(DriverService.AddDriver(name, CurrentCompany.ID));
            })
            { IsBackground = true }.Start();
            return result.Task;
        }

        private async void ExecDeleteDriver(object parameters)
        {
            if (parameters is Driver d)
            {
                bool result = await DeleteDriverAsync(d.ID);
                if (result)
                {
                    new TipWindow("提示", "删除成功！").Show();
                    CurrentCompanyDrivers = await GetDriversByCompanyIDAsync();
                }
                else
                    new TipWindow("错误", "删除失败。请检查网络连接或用户权限").Show();
            }
            else
                new TipWindow("提示", "请先选择一个要取消认证的员工").Show();
        }

        private Task<bool> DeleteDriverAsync(int id)
        {
            var result = new TaskCompletionSource<bool>();
            new Thread(() =>
            {
                result.SetResult(DriverService.DeleteDriver(id));
            })
            { IsBackground = true }.Start();
            return result.Task;
        }

        private async void ExecAddCar(object parameters)
        {
            if (Lic != string.Empty && float.TryParse(Load, out float l))
            {
                int? r = await AddCarAsync(Lic, l);
                if (r == null)
                    new TipWindow("警告", "添加失败。请检查网络连接或用户权限").Show();
                else
                {
                    new TipWindow("提示", $"添加成功，汽车ID为{r}").Show();
                    CurrentCompanyCars = await GetCarsByCompanyIDAsync();
                }
            }
            else
                new TipWindow("提示", "请填入正确的参数").Show();
        }

        private Task<int?> AddCarAsync(string lic, float load)
        {
            var result = new TaskCompletionSource<int?>();
            new Thread(() =>
            {
                result.SetResult(CarService.AddCar(CurrentCompany.ID, load, lic));
            })
            { IsBackground = true }.Start();
            return result.Task;
        }

        private async void ExecDeleteCar(object parameters)
        {
            if (parameters is Car d)
            {
                bool result = await DeleteCarAsync(d.ID);
                if (result)
                {
                    new TipWindow("提示", "删除成功！").Show();
                    CurrentCompanyCars = await GetCarsByCompanyIDAsync();
                }
                else
                    new TipWindow("错误", "删除失败。请检查网络连接或用户权限").Show();
            }
            else
                new TipWindow("提示", "请先选择一个要取消认证的汽车").Show();
        }

        private Task<bool> DeleteCarAsync(int id)
        {
            var result = new TaskCompletionSource<bool>();
            new Thread(() =>
            {
                result.SetResult(CarService.DeleteCar(id));
            })
            { IsBackground = true }.Start();
            return result.Task;
        }
    }
}
